colorAdminApp.controller('departmentManageController', function($scope,$compile,$rootScope,$http,$state,$stateParams,modalService) {
	
	var  className = "dark";
	
	
	
	function beforeEditName(treeId, treeNode) {
		className = (className === "dark" ? "":"dark");
		var zTree = $.fn.zTree.getZTreeObj("ztree");
		zTree.selectNode(treeNode);
		$scope.editEntity["nameZh"]=treeNode.nameZh;
		$scope.editEntity["nameEn"]=treeNode.nameEn;
		$scope.editEntity["code"]=treeNode.code;
		$scope.editEntity["seqNum"]=treeNode.seqNum;
		$scope.editEntity["id"]=treeNode.id;
		$scope.editEntity["parentId"]=(treeNode.pId==null?0:treeNode.pId);
		$scope.$apply();//更新数据
		$('#editModal').modal('show');
		return false;//false表示不显示编辑状态
	}
	
	function beforeRemove(treeId, treeNode) {
		className = (className === "dark" ? "":"dark");
		var zTree = $.fn.zTree.getZTreeObj("ztree");
		zTree.selectNode(treeNode);
		if(confirm("确认删除 " + treeNode.name + " 吗？")){
			$scope.doDelete(treeNode.id);
		}
		return false;
	}
	
	function onRemove(e, treeId, treeNode) {
		
	}
	function beforeRename(treeId, treeNode, newName, isCancel) {
		className = (className === "dark" ? "":"dark");
		if (newName.length == 0) {
			alert("节点名称不能为空.");
			var zTree = $.fn.zTree.getZTreeObj("ztree");
			setTimeout(function(){zTree.editName(treeNode)}, 10);
			return false;
		}
		return true;
	}
	function onRename(e, treeId, treeNode, isCancel) {
		
	}
	/**
	 * 是否显示删除按钮
	 * @param treeId
	 * @param treeNode
	 * @returns {Boolean}
	 */
	function showRemoveBtn(treeId, treeNode) {
		if(treeNode.id==1){
			return false;
		}
		return true;
	}
	/**
	 * 是否显示编辑按钮
	 * @param treeId
	 * @param treeNode
	 * @returns {Boolean}
	 */
	function showRenameBtn(treeId, treeNode) {
		return true;
	}

	function addHoverDom(treeId, treeNode) {
		var sObj = $("#" + treeNode.tId + "_span");
		if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
		
		var addStr = "<span class='button add' id='addBtn_" + treeNode.tId+ "' title='添加' onfocus='this.blur();'></span>";
		sObj.after(addStr);
		var btn = $("#addBtn_"+treeNode.tId);
		if (btn) btn.bind("click", function(){
			$scope.entity["parentId"]=treeNode.id;
			$('#addModal').modal('show');
			return false;
		});
	};
	function removeHoverDom(treeId, treeNode) {
		$("#addBtn_"+treeNode.tId).unbind().remove();
	};
	
	function initTree(treeId,data){
		var setting = {
				view: {
					addHoverDom:addHoverDom,
					removeHoverDom: removeHoverDom,
					selectedMulti: false
				},
				edit: {
					enable: true,
					editNameSelectAll: true,
					showRemoveBtn: showRemoveBtn,
					showRenameBtn: showRenameBtn
				},
				data: {
					simpleData: {
						enable: true
					}
				},
				callback: {
					beforeEditName: beforeEditName,
					beforeRemove: beforeRemove,
					beforeRename: beforeRename,
					onRemove: onRemove,
					onRename: onRename
				}
			};
		
		$.fn.zTree.init($("#"+treeId),setting,data);
	};
	
	
	
	
	$scope.loadData=function(){
		var submitData={};
		$(".panel-body").block({ message:'正在加载数据，请稍等...',css: { top: '50%' } });
		$http.post('api/core/access/department/getZTreeJSON',{params:submitData}).success(function(data, status, headers, config) {
			initTree("ztree",data.data);
			$(".panel-body").unblock();
		}).error(function(data, status, headers, config) {
			$(".panel-body").unblock();
	    	
        });  
	}
	
	    $scope.save=function(entity){
				entity["nameEn"]=entity["code"];
				var requestJson=JSON.stringify(entity);
				$(".modal-dialog").block({ message:'正在执行操作，请稍等...',css: { top: '50%' } });
				$http.post('api/core/access/department/add',{params:{"requestJson":requestJson}}).success(function(data, status, headers, config) {
					modalService.showAlertModal('注意',data.message);
					$(".modal-dialog").unblock();
					if(data.status==1){
						 $scope.loadData(); 
					     $('#addModal').modal('hide');  
					}
				}).error(function(data, status, headers, config) {
					$(".modal-dialog").unblock();
			    
		       });  
		};
			
			
			
		     $scope.update=function(editEntity){
		    	editEntity["nameEn"]=editEntity["code"];
		    	var requestJson=JSON.stringify(editEntity);
		    	$(".modal-dialog").block({ message:'正在执行操作，请稍等...',css: { top: '50%' } });
		 		$http.post('api/core/access/department/update',{params:{"requestJson":requestJson}}).success(function(data, status, headers, config) {
		 			modalService.showAlertModal('注意',data.message);
		 			$(".modal-dialog").unblock();
		 			if(data.status==1){
		 					$scope.loadData(); 
		 					$('#editModal').modal('hide');  
		 			}
		 		}).error(function(data, status, headers, config) {
		 			$(".modal-dialog").unblock();
		 	    	
		        });
			};
			
			$scope.doDelete=function(id){
				$(".modal-dialog").block({ message:'正在执行操作，请稍等...',css: { top: '50%' } });
				$http.post('api/core/access/department/deleteById',{params:{"id":id}}).success(function(data, status, headers, config) {
					modalService.showAlertModal('注意',data.message);
					$(".modal-dialog").unblock();
					if(data.status==1){
						$scope.loadData(); 
					}
				}).error(function(data, status, headers, config) {
					$(".modal-dialog").unblock();
			    	
		       });  
			}
			
			$scope.showAddRootResourceModal=function(){
				$scope.entity.parentId=0;
				$('#addModal').modal('show');
			}
			
		    $scope.entity={};
		    $scope.editEntity={};
		  	$scope.loadData();//加载数据
			
});
